package com.xinan.weixin.menu;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.BaseController;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.MenuApi;
import com.jfinal.weixin.sdk.api.ReturnCode;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xiaojing
 * Date: 14-10-31
 * Time: 上午11:49
 * To change this template use File | Settings | File Templates.
 */
public class MenuController extends BaseController {
    // 菜单创建（POST）
    private static String createMenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
    // 菜单查询（GET）
    private static String getMenu = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=";
    // 菜单删除（GET）
    private static String delMenu = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";

    /**
     * 获取公众号菜单
     */
    public void getMenu() {
        String jsonStr="{\n" +
                "     \"button\":[\n" +
                "     {\t\n" +
                "          \"type\":\"click\",\n" +
                "          \"name\":\"今日歌曲\",\n" +
                "          \"key\":\"V1001_TODAY_MUSIC\"\n" +
                "      },\n" +
                "      {\n" +
                "           \"name\":\"菜单\",\n" +
                "           \"sub_button\":[\n" +
                "           {\t\n" +
                "               \"type\":\"view\",\n" +
                "               \"name\":\"搜索\",\n" +
                "               \"url\":\"http://www.soso.com/\"\n" +
                "            },\n" +
                "            {\n" +
                "               \"type\":\"view\",\n" +
                "               \"name\":\"视频\",\n" +
                "               \"url\":\"http://v.qq.com/\"\n" +
                "            },\n" +
                "            {\n" +
                "               \"type\":\"click\",\n" +
                "               \"name\":\"赞一下我们\",\n" +
                "               \"key\":\"V1001_GOOD\"\n" +
                "            }]\n" +
                "       }]\n" +
                " }";
        ApiResult apiResult = MenuApi.getMenu();
        createMenu(jsonStr);
        if (apiResult.isSucceed())
            renderText(apiResult.getJson());
        else
            renderText(apiResult.getErrorMsg());
    }



    /**
     * 创建自定义菜单
     */
    public void setMenu()
    {
        String menus="{\"button\":[";
        String menua="";
        MenuApi menuApi=new MenuApi();
        //ApiResult apiResult = menuApi.createMenu("{type: 'view',name: '点餐',url: 'http://loveyy.gnjsp.xzbiz.cn/_front/main.html/',sub_button: [ ]},{type: 'view',name: '我的订单',url: 'http://www.baidu.com',sub_button: [ ]},{type: 'view',name: '其他',url: 'http://www.baidu.com',sub_button: [ ]}]}}");
        List<Record> newlist =   WxMenu.dao.findbymenu(" where level=1");
        for ( int i=0;i<newlist.size();i++){
         String name=   newlist.get(i).get("name");
         String id=   newlist.get(i).get("id");
         String type=   newlist.get(i).get("type");
         String key=   newlist.get(i).get("key");
         String url=   newlist.get(i).get("url");
         String num=   newlist.get(i).get("num").toString();
            if( !num.equals("0")){
                menua+= "{\"name\":\""+name+"\"," ;
                List<Record> newlists =   WxMenu.dao.findbymenu(" where level=1  and pid= '"+id+"'");
                String names=   newlist.get(i).get("name");
                String ids=   newlist.get(i).get("id");
                String types=   newlist.get(i).get("type");
                String keys=   newlist.get(i).get("key");
                String urls=   newlist.get(i).get("url");
                String nums=   newlist.get(i).get("num").toString();
                menua+= "\"sub_button\":[{\"type\":\""+keys+"\",\"name\":\""+names+"\",\"key\":\""+keys+"\",\"url\":\""+urls+"\"}," ;
                menua+="]}";
            }   else {
                menua+= "{\"type\":\""+type+"\",\"name\":\""+name+"\",\"key\":\""+key+"\",\"url\":\""+url+"\"}," ;
            }
            System.out.println(menua);
            menus=menua+"]}";
            System.out.println(menus);

        }


        ApiResult apiResult = menuApi.createMenu(menus);
        System.out.println("======================"+apiResult.getErrorCode());
//        String result= ReturnCode.get(0);
        renderText(apiResult.getJson());
    }

    /**
     * 创建菜单
     */
    public ApiResult createMenu(String jsonStr) {
        String jsonResult = HttpKit.post(createMenu + AccessTokenApi.getAccessToken().getAccessToken(), jsonStr);
        return new ApiResult(jsonResult);
    }
}
